import type { StateCreator } from 'zustand';
import type { SystemStore } from '../index';

// 用户角色类型
export type UserRole = 'admin' | 'user' | 'guest';

// 用户状态类型
export type UserStatus = 'active' | 'inactive' | 'banned';

// 用户信息接口
export interface UserInfo {
  id: string;
  username: string;
  email: string;
  avatar?: string;
  role: UserRole;
  status: UserStatus;
  createdAt: string;
  lastLoginAt?: string;
}

// 用户偏好设置
export interface UserPreferences {
  notifications: boolean;
  emailUpdates: boolean;
  timezone: string;
  dateFormat: string;
}

// 用户状态接口
export interface UserState {
  userInfo: UserInfo | null;
  isAuthenticated: boolean;
  isLoggingIn: boolean;
  preferences: UserPreferences;
  permissions: string[];
}

// 用户Actions接口
export interface UserActions {
  // 认证相关
  setUserInfo: (userInfo: UserInfo | null) => void;
  setAuthenticated: (authenticated: boolean) => void;
  setLoggingIn: (logging: boolean) => void;
  login: (userInfo: UserInfo) => void;
  logout: () => void;
  
  // 用户偏好
  setPreferences: (preferences: Partial<UserPreferences>) => void;
  updatePreference: <K extends keyof UserPreferences>(
    key: K,
    value: UserPreferences[K]
  ) => void;
  
  // 权限管理
  setPermissions: (permissions: string[]) => void;
  addPermission: (permission: string) => void;
  removePermission: (permission: string) => void;
  hasPermission: (permission: string) => boolean;
  
  // 用户信息更新
  updateUserInfo: (updates: Partial<UserInfo>) => void;
  
  // 重置用户状态
  resetUserState: () => void;
}

// 用户Slice类型
export type UserSlice = UserState & UserActions;

// 默认用户偏好设置
const defaultPreferences: UserPreferences = {
  notifications: true,
  emailUpdates: false,
  timezone: 'Asia/Shanghai',
  dateFormat: 'YYYY-MM-DD',
};

// 默认状态
const initialUserState: UserState = {
  userInfo: null,
  isAuthenticated: false,
  isLoggingIn: false,
  preferences: defaultPreferences,
  permissions: [],
};

// 创建用户Slice
export const createUserSlice: StateCreator<
  SystemStore,
  [['zustand/immer', never], ['zustand/devtools', never], ['zustand/persist', unknown], ['zustand/subscribeWithSelector', never]],
  [],
  UserSlice
> = (set, get) => ({
  // 初始状态
  ...initialUserState,

  // Actions
  setUserInfo: (userInfo) => {
    set((state: SystemStore) => {
      state.userInfo = userInfo;
    });
  },

  setAuthenticated: (authenticated) => {
    set((state: SystemStore) => {
      state.isAuthenticated = authenticated;
    });
  },

  setLoggingIn: (logging) => {
    set((state: SystemStore) => {
      state.isLoggingIn = logging;
    });
  },

  login: (userInfo) => {
    set((state: SystemStore) => {
      state.userInfo = userInfo;
      state.isAuthenticated = true;
      state.isLoggingIn = false;
    });
  },

  logout: () => {
    set((state: SystemStore) => {
      state.userInfo = null;
      state.isAuthenticated = false;
      state.isLoggingIn = false;
      state.permissions = [];
    });
  },

  setPreferences: (preferences) => {
    set((state: SystemStore) => {
      state.preferences = { ...state.preferences, ...preferences };
    });
  },

  updatePreference: (key, value) => {
    set((state: SystemStore) => {
      state.preferences[key] = value;
    });
  },

  setPermissions: (permissions) => {
    set((state: SystemStore) => {
      state.permissions = permissions;
    });
  },

  addPermission: (permission) => {
    set((state: SystemStore) => {
      if (!state.permissions.includes(permission)) {
        state.permissions.push(permission);
      }
    });
  },

  removePermission: (permission) => {
    set((state: SystemStore) => {
      state.permissions = state.permissions.filter((p: string) => p !== permission);
    });
  },

  hasPermission: (permission) => {
    return get().permissions.includes(permission);
  },

  updateUserInfo: (updates) => {
    set((state: SystemStore) => {
      if (state.userInfo) {
        state.userInfo = { ...state.userInfo, ...updates };
      }
    });
  },

  resetUserState: () => {
    set((state: SystemStore) => {
      Object.assign(state, initialUserState);
    });
  },
}); 